import axios from 'axios'

const request = axios.create({
  baseURL: '/api', // 因为我本地做了反向代理
  timeout: 60000,
  withCredentials: true, // 是否允许带cookie这些
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
  }
})

// 传参序列化(添加请求拦截器)
request.interceptors.request.use(
  config => {
    return config
  },
  error => {
    return Promise.reject(error.data.error.message)
  }
)

export function get (url, params = {}) {
  return new Promise((resolve, reject) => {
    request
      .get(url, { params: params })
      .then(response => {
        resolve(response.data)
      })
      .catch(err => {
        reject(err)
      })
  })
}
